package whu.iss.web.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import whu.iss.services.support.BaseServices;


public abstract class ControllerSupport implements BaseController 
{
	
	
	/**********************************************************************
	 *      ҵ���߼�������ط���
	 **********************************************************************/
	protected BaseServices services=null;
	
	
	//------------------------��һʵ����ѯ------------------------------------
	protected final void saveInstanceById()throws Exception
	{
		Map<String,String> ins=this.services.findById();
		if(ins!=null)
		{
			this.addAttribute("ins", ins);
		}
		else
		{
			this.addAttribute("msg", "��������ɾ�����ֹ����!");
		}	
	}
	
	//---------------------���ݲ�ѯ�ķ�װ------------------------------
	protected final void savePageData()throws Exception
	{
		this.savePageData("rows", "msg");
	}
	
	/**
	 * ���ݲ�ѯ����
	 * @param dateName   --- ��ѯ������ݼ���������
	 * @param messageName --- ��Ϣ��ʾ������������
	 * @throws Exception
	 */
	protected final void savePageData(String dateName,String messageName)throws Exception
	{
		List<Map<String,String>> rows=this.services.query();
		if(rows.size()>0)
		{
			this.addAttribute(dateName, rows);
		}
		else
		{
			this.addAttribute(messageName, "û�з�������������!");
		}	
	}
	
	protected final void queryForDelete()throws Exception
	{
		 this.queryForDelete("rows");
	}
	
	protected final void queryForDelete(String dateName)throws Exception
	{
		List<Map<String,String>> rows=this.services.query();
		if(rows.size()>0)
		{
			this.addAttribute(dateName, rows);
		}
	}
	//------------------------���ݸ��µķ�װ------------------------
	protected void update(String utype,String messageText)throws Exception
	{
		String msg=this.services.update(utype)?"�ɹ�!":"ʧ��!";
		this.addAttribute("msg", messageText+msg);
	}
	
	/**********************************************************************
	 *      ���Դ�����
	 **********************************************************************/
	/**
	 * װ�����е�����
	 */
	private Map<String,Object> attributes=new HashMap<>();
	/**
	 * ҵ�������,������Եķ���
	 * @param key
	 * @param value
	 */
	protected void addAttribute(String key,Object value)
	{
		this.attributes.put(key, value);
	}
	@Override
	public Map<String, Object> getAttributes() 
	{
		return this.attributes;
	}
	
	@Override
	public void clearAttributes() 
	{
		this.attributes.clear();
	}
	
	/****************************************************
	 *     ��������
	 ****************************************************/
    private Map<String,Object> dto=null;
	@Override
	public void setDto(Map<String, Object> dto) 
	{
	    this.dto=dto;
	    this.services.setMapDto(dto);
	}
	/**
	 * �����ȡDTO����
	 * @return
	 */
	protected final Map<String,Object> getDto()
	{
		return this.dto;
	}
	/**
	 * �����ȡDTO��ĳ������
	 * @param key
	 * @return
	 */
	protected final Object getValue(String key)
	{
		return this.dto.get(key);
	}
	/**
	 * ������ʾDTO
	 */
	protected final void showDto()
	{
		System.out.println(this.dto);
	}
	/**
	 * ������DTO��������
	 * @param key
	 * @param value
	 */
	protected final void putDto(String key,Object value)
	{
		this.dto.put(key, value);
	}
	

}
